import auth, { AuthParams, UserPermission } from '@/utils/authentication';
import React,{ useEffect, useMemo, useState } from 'react';
import {ApartmentOutlined, BellOutlined, CrownFilled, GlobalOutlined, HomeOutlined, InsuranceOutlined, NodeIndexOutlined, SafetyCertificateOutlined, SettingOutlined, UserOutlined, UserSwitchOutlined} from '@ant-design/icons'
export type IRoute = AuthParams & {
  name: string;
  path: string;
  // 当前页是否展示面包屑
  breadcrumb?: boolean;
  children?: IRoute[];
  // 当前路由是否渲染菜单项，为 true 的话不会在菜单中显示，但可通过路由地址访问。
  ignore?: boolean;
  icon?:any

};

export const routes: IRoute[] = [
  {
    name: '首页',
    path: '/workbench/dashboard/workplace',
    icon:  <HomeOutlined />,
    
   
  },
  {
    name: '交付',
    path: '/workbench/deliver',
    ignore:true,
    icon:  <CrownFilled />,
    children: [
      {
        name:'云效',
        icon:  <CrownFilled />,
        path:'/workbench/deliver/codeup/list'
      },
      {
        name: '项目列表',
        ignore:true,
        path: '/workbench/deliver/detail',
      },
      {
        name: '项目详情',
        ignore:true,
        path: '/workbench/deliver/project/detail',
      },
      {
        name: '发布计划',
        ignore:true,
        path: '/workbench/deliver/project/publishPlan',
      },
      
    ],
  },
  {
    name: '监控',
    path: '/workbench/monitor/error',
    ignore:true,
    icon:  <CrownFilled />,
    children: [
      {
        name: '错误',
        icon:  <CrownFilled />,

        path: '/workbench/monitor/error',
      },
      {
        name: '性能',
        icon:  <CrownFilled />,

        path: '/workbench/monitor/perf',
      },
      {
        name: '来源',
        icon:  <CrownFilled />,

        path: '/workbench/monitor/source',
      },
      
    ],
  },
  {
    name: '设置',
    icon: <SettingOutlined />,
    path: '/workbench/globalSetting',
    children: [
      {
        name: '站点',
        icon: <GlobalOutlined />,
        path: '/workbench/globalSetting/site',
      },
      {
        name: '流水线',
        icon: <ApartmentOutlined />,
        path: '/workbench/globalSetting/pipeline',
      },
      {
        name: '凭证',
        icon: <SafetyCertificateOutlined />,
        path: '/workbench/globalSetting/certificate',
      },
      {
        name: '通知',
        icon: <BellOutlined />,
        path: '/workbench/globalSetting/notice',
      },
      // {
      //   name: '映射',
      //   icon:<NodeIndexOutlined />,
      //   path: '/globalSetting/mapping',
      // },
      {
        name: '用户',
        icon:<UserSwitchOutlined />,
        path: '/workbench/globalSetting/account',
      },
      {
        name: '权限',
        icon:<UserSwitchOutlined />,
        path: '/workbench/globalSetting/authority',
      },
    ],
  },
  {
    name: '个人',
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    icon:  <UserOutlined />,
    
    path: '/workbench/user',
    // children: [
    //   {
    //     name: '用户信息',
    //     path: '/user/info',
    //   },
    //   {
    //     name: '用户设置',
    //     path: '/user/settings',
    //   },
    // ],
  },
];

export const getName = (path: string, routes: any[]) => {
  return routes.find((item: { key: any; name: any; children: any; }) => {
    const itemPath = `/${item.key}`;
    if (path === itemPath) {
      return item.name;
    } else if (item.children) {
      return getName(path, item.children);
    }
  });
};

export const generatePermission = (role: string) => {
  const actions = role === 'admin' ? ['*'] : ['read'];
  const result = {};
  routes.forEach((item) => {
    if (item.children) {
      item.children.forEach((child) => {
        result[child.name] = actions;
      });
    }
  });
  return result;
};

const useRoute = (userPermission: never[] | UserPermission): [IRoute[], string] => {
  const filterRoute = (routes: IRoute[], arr = []): IRoute[] => {
    if (!routes.length) {
      return [];
    }
    for (const route of routes) {
      const { requiredPermissions, oneOfPerm } = route;
      let visible = true;
      if (requiredPermissions) {
        visible = auth({ requiredPermissions, oneOfPerm }, userPermission);
      }

      if (!visible) {
        continue;
      }
      if (route.children && route.children.length) {
        const newRoute = { ...route, children: [] };
        filterRoute(route.children, newRoute.children);
        if (newRoute.children.length) {
          arr.push(newRoute);
        }
      } else {
        arr.push({ ...route });
      }
    }

    return arr;
  };

  const [permissionRoute, setPermissionRoute] = useState(routes);

  useEffect(() => {
    const newRoutes = filterRoute(routes);
    setPermissionRoute(newRoutes);
  }, [JSON.stringify(userPermission)]);

  const defaultRoute = useMemo(() => {
    const first = permissionRoute[0];
    if (first) {
      const firstRoute = first?.children?.[0]?.path || first.path;
      return firstRoute;
    }
    return '';
  }, [permissionRoute]);

  return [permissionRoute, defaultRoute];
};

export default useRoute;
